package com.vogue.supplymall.order.domain.provider;

import com.vogue.ssm.database.provider.BaseSqlProvider;
import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.common.domain.dto.CommentInfo;
import org.apache.commons.lang3.StringUtils;

public class CommentProvider extends BaseSqlProvider{


    /**
     * 买家评论列表
     * @param commentInfo
     * @return
     */
    public String find(CommentInfo commentInfo) {
        StringBuilder sql = new StringBuilder();

        if (commentInfo.getCommentstate() == 0) {
            //待评价
            sql = selectCommenting(commentInfo);
        } else {
            //已评价
            sql = selectFinishComment(commentInfo);
        }
        return DealStatus.formatShadeSql(sql.toString());
    }

    /**
     * 卖家评论列表
     * @param commentInfo
     * @return
     */
    public String findSell(CommentInfo commentInfo) {
        StringBuilder sql = new StringBuilder();
        //查询状态  0：待回复  1：已回复 2: 不回复 3: 回复
        int commentstate = commentInfo.getCommentstate();
        String sqlWhr = "";
        if (commentstate == 1) {
            sqlWhr = " AND c.answerflag = 1 and d.answerflag = 1";
        } else if (commentstate == 2) {
            sqlWhr = " AND c.restoreflag = 2 and c.answerflag = 0";
        } else if (commentstate == 3) {
            sqlWhr = " and c.answerflag = 0 AND c.restoreflag = 1 ";
        } else {
            sqlWhr = " and c.answerflag = 0 AND (c.restoreflag = 0 or c.restoreflag = 1) ";
        }

        sql.append("SELECT c.commentmark,d.commenttext,c.buyername,c.ordernumber,c.orderdate,c.productname,c.productimgurl,c.restoreflag,")
                .append("c.buyerid,c.orderid,c.id commentid,c.productid ")
                .append("FROM `comment` c,comment_detail d ")
                .append("where c.id = d.commentid and d.commentobejct = 0 ")
                .append(" and c.sellersyscodeid = #{syscodeid} ")
                .append(" and c.shopid = #{shopid} ")
                .append(sqlWhr);

        //显示一年前到当前时间的数据
        sql.append(" AND c.orderdate >= date_sub(curdate(),interval 1 year) and c.orderdate <= NOW() ");
        // 匹配搜索内容
        if (StringUtils.isNotEmpty(commentInfo.getKeyword())) {
            sql.append(" AND ( c.ordernumber LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR c.buyername LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR c.productname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR d.commenttext LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" ) ");
        }
        //下单开始时间
        if (StringUtils.isNotEmpty(commentInfo.getBeginDate())) {
            sql.append(" AND c.orderdate >= #{beginDate} ");
        }
        //下单结束时间
        if (StringUtils.isNotEmpty(commentInfo.getEndDate())) {
            sql.append("  AND c.orderdate <= #{endDate}  ");
        }

        sql.append(" ORDER BY c.orderdate DESC ")
                .append(" LIMIT #{start},#{length} ");

        return DealStatus.formatShadeSql(sql.toString());
    }

    /**
     * 待评价
     * @param commentInfo
     * @return
     */
    private StringBuilder selectCommenting(CommentInfo commentInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" o.ordernumber,p.pname productname,o.createdate orderdate,os.shopname,p.imgurl,")
                .append(" os.shopid,o.id orderid,p.productid ,os.syscodeid sellersyscodeid ")
                .append(" from order_item_product p,`order` o,order_seller os,order_buyer ob,order_status ost ")
                .append(" WHERE p.commentstate = 0 ")
                .append(" and p.disabledflag = 0")
                .append(" and o.id = p.orderid ")
                .append(" and o.id = os.orderid ")
                .append(" and ost.orderstatus = 4 ")
                .append(" and ost.orderid = o.id ")
                .append(" and o.id = ob.orderid ")
                .append(" and ob.syscodeid = #{syscodeid} ")
                .append(" and ob.organid = #{organid} ")
                .append(" and ob.userid = #{loginId} ")
        ;

        //显示一年前到当前时间的数据
        sql.append(" and o.createdate >= date_sub(curdate(),interval 1 year) and  o.createdate <= NOW() ");
        // 匹配搜索内容
        if (StringUtils.isNotEmpty(commentInfo.getKeyword())) {
            sql.append(" AND ( o.ordernumber LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR os.shopname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR p.pname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" ) ");
        }
        //下单开始时间
        if (StringUtils.isNotEmpty(commentInfo.getBeginDate())) {
            sql.append(" AND o.createdate >= #{beginDate} ");
        }
        //下单结束时间
        if (StringUtils.isNotEmpty(commentInfo.getEndDate())) {
            sql.append("  AND o.createdate <= #{endDate}  ");
        }

        sql.append(" ORDER BY o.createdate DESC,o.id  ")
                .append(" LIMIT #{start},#{length} ");

        return sql;
    }

    /**
     * 已评价--按照买家评价明细修改时间排序
     * @param commentInfo
     * @return
     */
    private StringBuilder selectFinishComment(CommentInfo commentInfo) {
        StringBuilder sql = new StringBuilder();

        sql.append(" SELECT ")
                .append(" c.ordernumber,c.productname,c.orderdate,c.shopname,c.productimgurl imgurl,")
                .append(" c.shopid,c.orderid,c.productid,c.id commentid,c.sellersyscodeid ")
                .append(" from order_buyer ob,`comment` c ")
                .append( "LEFT JOIN (SELECT commentid,modifydate from comment_detail where commentobejct = 0) d on c.id = d.commentid ")
                .append("WHERE c.orderid = ob.orderid ")
                .append(" and ob.syscodeid = #{syscodeid} ")
                .append(" and ob.organid = #{organid} ")
                .append(" and ob.userid = #{loginId} ")
        ;
        //显示一年前到当前时间的数据
        sql.append(" and c.orderdate >= date_sub(curdate(),interval 1 year) and c.orderdate <= NOW() ");

        // 匹配搜索内容
        if (StringUtils.isNotEmpty(commentInfo.getKeyword())) {
            sql.append(" AND ( c.ordernumber LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR c.shopname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR c.productname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" ) ");
        }
        //下单开始时间
        if (StringUtils.isNotEmpty(commentInfo.getBeginDate())) {
            sql.append(" AND c.orderdate >= #{beginDate} ");
        }
        //下单结束时间
        if (StringUtils.isNotEmpty(commentInfo.getEndDate())) {
            sql.append("  AND c.orderdate <= #{endDate}  ");
        }

        sql.append(" ORDER BY d.modifydate DESC,c.orderid ")
            .append(" LIMIT #{start},#{length} ");
        return sql;
    }

}
